package 线程.zuoye;

public class zuoye3 implements Runnable {
    public static int num=0;//第几章票
    public  static int count=10;
    public static boolean fiag=false;//
    public synchronized void sale(){
        if (count<=0){
            fiag=true;
            return;
        }

        num++;
        count--;
        if (Thread.currentThread().getName().equals("黄牛")){
            return;
        }
        System.out.println(Thread.currentThread().getName()+"买到了第"+num+"张票剩余"+count+"张票");

    }

    public void run(){
        while (true){
            synchronized (this){
                if (count<=0){
                    break;
                }
            }
            if (count<=0){
                fiag=true;
                return;
            }

            num++;
            count--;
            if (Thread.currentThread().getName().equals("黄牛")){
                return;
            }
            System.out.println(Thread.currentThread().getName()+"买到了第"+num+"张票剩余"+count+"张票");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /*sale();*/
        }
    }
}
